package com.sgq.vue.utils.resource;

import com.sgq.vue.entity.resource.City;
import com.sgq.vue.entity.resource.Weather;
import com.sgq.vue.entity.resource.WeatherApi;
import com.sgq.vue.enums.RequestConditionEnums;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 83778
 * @date: 2024-02-29 16:35
 * @desc:
 */
public class RequestUrlBuilder {

    /**
     *  用于构建请求，最终留一个查询城市的条件  cityId 或者 cityName
     *  产生的请求参数只有 secretKey
     * @param weatherApi
     * @return
     */
    public static String buildRequestUrl(WeatherApi weatherApi) {
        StringBuilder requestUrl = new StringBuilder();
        //为了区别 url 后面 跟 ? 还是 &
        boolean appIdKeyIsSet = false;
        String appUrl = weatherApi.getAppUrl();
        String appIdKey = weatherApi.getAppIdKey();
        String appSecretKey = weatherApi.getAppSecretKey();
        String conditionKey = weatherApi.getConditionKey();
        requestUrl.append(appUrl);
        //如果是restful风格的请求，只用直接返回 appUrl 即可
        if(appUrl.endsWith("/")){
            return requestUrl.toString();
        }
        if (!ObjectUtils.isEmpty(appIdKey)){
            requestUrl.append("?");
            requestUrl.append(appIdKey);
            requestUrl.append("=");
            String appIdValue = weatherApi.getAppIdValue();
            requestUrl.append(appIdValue);
            appIdKeyIsSet = true ;
        }
        if (!ObjectUtils.isEmpty(appSecretKey)){
            if (appIdKeyIsSet){
                requestUrl.append("&");
            }else {
                requestUrl.append("?");
            }
            requestUrl.append(appSecretKey);
            requestUrl.append("=");
            String appSecretValue = weatherApi.getAppSecretValue();
            requestUrl.append(appSecretValue);
        }

        return requestUrl.toString();
    }

    public static List<Weather> buildCondition(List<City> cityList, WeatherApi weatherApi) {
        //获取带有secretKey的requestUrl
        String requestUrl = RequestUrlBuilder.buildRequestUrl(weatherApi);

        String [] conditionTypes = weatherApi.getConditionType().split(",");
        String [] conditionKeys = weatherApi.getConditionKey().split(",");

        List <Weather> weathers = new ArrayList<>();
        for (City city : cityList) {
            StringBuilder sb = new StringBuilder(requestUrl);
            Weather weather = new Weather();
            weather.setCityId(city.getId());
            weather.setCity(city.getCityZh());
            sb.append("&");
            //默认取第一种条件 如果时城市代码url则是
            if (Integer.valueOf(conditionTypes[0]).equals(RequestConditionEnums.CITY_CODE.getTypeId())){
                sb.append(conditionKeys[0]);
                sb.append("=");
                sb.append(city.getId());
            }else  if (Integer.valueOf(conditionTypes[0]).equals(RequestConditionEnums.CITY_EN.getTypeId())){
                sb.append(conditionKeys[0]);
                sb.append("=");
                sb.append(city.getCityEn());
            }else  if (Integer.valueOf(conditionTypes[0]).equals(RequestConditionEnums.CITY_ZH.getTypeId())){
                sb.append(conditionKeys[0]);
                sb.append("=");
                sb.append(city.getCityZh());
            }
            //使用TagType临时存放请求地址
            weather.setTagType(sb.toString());
            weathers.add(weather);
            sb.setLength(0);
        }
        return weathers;
    }
}
